
package com.zjpavt.client.hal;

import com.zjpavt.client.enums.KeyCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * KeyPadSimulator
 * 键盘模拟器 用于在客户端模拟在设备上的按键效果
 * 且按键不计入Z指令返回
 *
 * @program: PavtFirmware
 * @description:
 * @author: zyc
 * @create: 2018-05-29 12:07
 **/
public class KeyPadSimulator {
    private static final Logger log = LoggerFactory.getLogger(KeyPadSimulator.class);
    private final List<KeyPad.KeyListener> listenerList = new ArrayList<>();

    public void notifyChanged(String keyValue) {
        KeyCodeEnum keyCodeEnum = KeyCodeEnum.getKeyCode(keyValue);
        if (keyCodeEnum == null) {
            log.error("unknown key value: " + keyValue);
            return;
        }
        List<KeyPad.KeyListener> copyList;
        synchronized (listenerList) {
            copyList = new ArrayList<>(listenerList);
        }
        for (KeyPad.KeyListener listener : copyList) {
            listener.onKeyPressed(keyCodeEnum);
        }
    }
    public void addKeyListener(KeyPad.KeyListener listener) {
        synchronized (listenerList) {
            listenerList.add(listener);
        }
    }
}
